<?php

/**
 * Created by PhpStorm.
 * User: qiliping
 * Date: 2017/6/17
 * Time: 16:52
 */
class User_withdrawal_model extends CI_Model
{
  function get_withdrawl_by_school($status = 'all',$school_id,$limit=0,$offset=10){
    $this->db->trans_start();
    $where = "w.school_id = '{$school_id}' ";
    if(!empty($status) && $status != 'all'){
      $where .= "AND w.status = '{$status}' ";
    }
    $sql = "select SQL_CALC_FOUND_ROWS w.*,u.nick,u.mobile from {$this->db->dbprefix}user_withdrawal w left join {$this->db->dbprefix}user u on u.id = w.user_id where $where order by id desc limit $limit,$offset";
    $result = $this->db->query($sql)->result_array();
    $row_num =  $this->db->query('SELECT FOUND_ROWS() as row_num;')->row_array();

    $this->db->trans_complete();

    return array("total_count" => $row_num['row_num'], "result" => $result);
  }

  function audit($id,$status){
    if($status==3){
      //拒绝审核
      $array = array('status' => 3,'audit_time'=>time() );
      $this->db->update('user_withdrawal',$array,array('id' => $id ));
    }else if($status ==2){
      $this->db->trans_start();
      //1更新状态（审核通过）
      $array = array('status' => 1,'audit_time'=>time() );
      $this->db->update('user_withdrawal',$array,array('id' => $id ));
      //2开始微信打钱

      //3开始扣钱
      
      //4更新状态（已打款）
      $array1 = array('status' => 1,'audit_time'=>time() );
      $this->db->trans_complete();
    }
  }

  function get_tixian_info($id){
    $sql = "SELECT tx.*, u.nick, au.balance, u.open_id FROM {$this->db->dbprefix}user_withdrawal AS tx ";
    $sql .= "LEFT JOIN {$this->db->dbprefix}user AS u ON(u.id = tx.user_id) ";
    $sql .= "LEFT JOIN {$this->db->dbprefix}user_account AS au ON(au.user_id = tx.user_id) WHERE tx.id='{$id}'";
    return $this->db->query($sql)->row_array();
  }
}
